/**
 * 
 */
package test;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JPanel;
import javax.swing.Timer;

/**
 * @author patrick
 *
 */
public class RTypeBoard extends JPanel implements ActionListener{

	/**
	 * generated
	 */
	private static final long serialVersionUID = 2503306633661631281L;

	private Timer timer;
	private Craft craft;
	
	public RTypeBoard () {
		
		addKeyListener(new TAdapter());
		setFocusable(true);
		setBackground(Color.black);
		setDoubleBuffered(true);
		
		craft = new Craft();
		
		timer = new Timer(5, this);
		timer.start();	
	}
	
	/**
	 * customized Keylistener
	 * */
	private class TAdapter extends KeyAdapter {
	
	    public void keyReleased(KeyEvent e) {
	        craft.keyReleased(e);
	    }
	
	    public void keyPressed(KeyEvent e) {
	        craft.keyPressed(e);
	    }
	}

	/**
	 * In the paint() method, we draw the spacecraft. 
	 * We get the image and the coordinates from the sprite class.
	 * */
	public void paint (Graphics g){
		
		super.paint(g);
		
		Graphics2D g2d = (Graphics2D) g;
		g2d.drawImage(craft.getImage(), craft.getX(), craft.getY(),this);
		
		Toolkit.getDefaultToolkit().sync();
		g.dispose();
		
	}
	
	/**
	 * The actionPerformed() method is called every 5ms. 
	 * We move the sprite and repaint the board.
	 * */
	public void actionPerformed(ActionEvent e) {
		craft.move();
		repaint();
	}

}
